#!/usr/bin/perl -w
#
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

use strict;
use Getopt::Long;
use HTML::FormFu::ConfigGenerator;
use Pod::Usage;

my $dsn;
my $duser;
my $dpassword;
my $name;
my $help;
my $schema;

GetOptions(
    'help|?'  => \$help,
    'dsn=s'     => \$dsn,
    'user=s'    => \$duser,
    'password=s'=> \$dpassword,
    'name=s'    => \$name,
    'schema=s' => \$schema,
);

pod2usage(2) if $help || ( ! ( $name && $dsn ) && ! $schema );

my $instance;
if ($schema) {
     eval "use $schema";
     $instance = $schema->connect();
}
else {
    package My::Schema;
    require DBIx::Class::Schema::Loader;
    no strict 'refs';
    use base qw/DBIx::Class::Schema::Loader/;
    My::Schema->loader_options(
        relationships => 1, 
        exclude => qr/^sqlite_sequence$/
    );
    $instance = My::Schema->connect($dsn, $duser, $dpassword);
}

my $generator = HTML::FormFu::ConfigGenerator->new( schema => $instance );
print $generator->config_for( $name );

__END__

=head1 NAME

formfu_config_gen.pl - Bootstrap a HTML::FormFu config

=head1 SYNOPSIS

formfu_config_gen.pl -name=RESULTSET_NAME [-dsn=DSN [ DSN OPTIONS ] | -schema='My::DBSchema' ] 

 Options:
   -help           display this help and exits
   -name           resultset name
   -schema         name of the DBIC schema class - it needs to be in the include path
   -dsn            dsn
   -user           database user
   -password       database password

 Examples:
    perl -Ilib -I t/lib/ scripts/formfu_config_gen.pl -name=Artist -schema=DBICTest::Schema 

    perl -Ilib scripts/formfu_config_gen.pl -name=Artist -dsn='dbi:SQLite:t/var/DBIxClass.db'




